<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-if="checkPermission(permissionRoles.distribution_vip_download)" :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
      <selectServer @server-selected="handleSelectServer" />
      创角天数:<el-input v-model="searchForm.create_actor_days" class="filter-item" style="width: 60px; margin-bottom: 5px;" placeholder="创角天数" />
      开服天数:<el-input v-model="searchForm.server_open_days" class="filter-item" style="width: 60px; margin-bottom: 5px;" placeholder="开服天数" />
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" :style="{height: '780px'}" class="mt-10" border height="780">
      <el-table-column label="等级" prop="level" width="79px" align="center"></el-table-column>
      <el-table-column v-for="sid in serverList" :key="sid" :label="sid + '服'" width="160px" align="center">
        <el-table-column label="停留人数" :prop="sid + '_num'" width="80px" align="center"></el-table-column>
        <el-table-column label="人数占比" :prop="sid + '_ratio'" width="80px" align="center"></el-table-column>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import SelectServer from '@/components/SelectServer'
import { getDistributionVipList } from '@/api/statis'
import checkPermission from '@/utils/permission'

export default {
  name: 'DistributionVip',
  components: { SelectServer },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      serverList: [],
      loading: false,
      listLoading: false,
      downloadLoading: false,
      searchForm: {
        create_actor_days: '1',
        server_open_days: '1',
        servers: []
      }
    }
  },
  created() {
  },
  methods: {
    checkPermission,
    handleFilter() {
      this.getList()
    },
    handleSelectServer(servers) {
      this.searchForm.servers = servers
    },
    async getList() {
      if (this.searchForm.servers.length < 1) {
        this.$message.error('请选择区服')
        return
      }
      if (this.searchForm.servers.length > 10) {
        this.$message.error('区服只能选择少于10个')
        return
      }
      if (this.searchForm.create_actor_days === '' || this.searchForm.create_actor_days === '0' || this.searchForm.create_actor_days > 7) {
        this.$message.error('创角天数只能是1-7')
        return
      }
      if (this.searchForm.server_open_days === '' || this.searchForm.server_open_days === '0' || this.searchForm.server_open_days > 180) {
        this.$message.error('开服天数只能是1-180')
        return
      }
      if (this.searchForm.server_open_days < this.searchForm.create_actor_days) {
        this.$message.error('创角天数不能大于开服天数')
        return
      }
      this.listLoading = true
      const res = await getDistributionVipList(this.searchForm)
      this.listLoading = false
      this.list = res.data.data
      this.serverList = res.data.sid
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const merges = ['A1:A2']
        const mh = ['等级']
        const tHeader = ['']
        const filterVal = ['level']
        let letter = 'A'
        this.serverList.forEach(sid => {
          const pl = String.fromCharCode(letter.charCodeAt(0) + 1)
          const al = String.fromCharCode(pl.charCodeAt(0) + 1)
          merges.push(pl + '1:' + al + '1')
          mh.push(sid + '服')
          mh.push('')
          tHeader.push('停留人数')
          tHeader.push('人数占比')
          filterVal.push(sid + '_num')
          filterVal.push(sid + '_ratio')
          letter = al
        })
        const multiHeader = [mh]

        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          multiHeader,
          header: tHeader,
          merges,
          data,
          filename: 'VIP等级分布'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>
